package cn.lr.org.service.impl;

import cn.lr.basic.Exception.BusinessException;
import cn.lr.basic.service.impl.BaseServiceImpl;
import cn.lr.basic.util.BaiduAuditUtils;
import cn.lr.basic.util.EmailUtil;
import cn.lr.basic.vo.ShopVo;
import cn.lr.org.domain.Employee;
import cn.lr.org.domain.Shop;
import cn.lr.org.domain.ShopAuditLog;
import cn.lr.org.mapper.EmployeeMapper;
import cn.lr.org.mapper.ShopAuditLogMapper;
import cn.lr.org.mapper.ShopMapper;
import cn.lr.org.service.IEmployeeService;
import cn.lr.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Shop shop) {
        shopMapper.save(shop);
    }

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    @Transactional
    public void update(Shop shop) {
        shopMapper.update(shop);
    }


    /**
     * 业务层逻辑
     *   一.参数校验
     *     1.参数不能为空
     *     2.店铺是否已经被注册
     *     非法校验：用智能审核（@TODO明天说）
     *
     *   二：管理员是否被注册
     *     1.管理员曾经已经注册，并且已经绑定过店铺
     *     2.管理员曾经已经注册，没有绑定过店铺
     *   三：保存数据
     *     1.保存员工
     *     2.保存店铺（设置店铺管理员并保存店铺）
     *     3.修改员工shopId
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        // 1.参数不能为空
        String name = shop.getName();
        if(StringUtils.isEmpty(name)){
           throw new BusinessException("店铺名不能为空！！"); // 抛出自定义异常
        }
        if(!BaiduAuditUtils.TextCensor(name)){
            throw new BusinessException("你好骚啊！都整违规了！");
        }
        // 2.店铺已经被注册
        Shop shopTemp = shopMapper.loadByName(name);
        if(shopTemp != null){
            throw new BusinessException("该店铺已被注册，请返回登录");
        }
        // 管理员是否被注册
        // 1.管理员曾经已经注册，并且已经绑定过店铺
        Employee admin = shop.getAdmin();
        Employee empTemp = employeeMapper.loadByUsername(admin.getUsername());
        if(empTemp != null && empTemp.getShop_id() != null ){
            throw new BusinessException("该管理员已被绑定，请更换其他管理员");
        }
       // 2.管理员曾经已经注册，没有绑定过店铺
        if (empTemp == null){ // 没有员工，新增
//            employeeMapper.save(admin); // 保存员工返回自增长id
            employeeService.save(admin);
            empTemp = admin;
        }
        shop.setAdmin_id(admin.getId()); // 保存店铺
        shopMapper.save(shop); // 返回自增长id
        empTemp.setShop_id(shop.getId());
        employeeMapper.update(empTemp); // 更新员工shop_id
    }

    @Override
    public void pass(ShopAuditLog log) {
        // 修改店铺状态
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(2);
        shopMapper.update(shop);
        // 保存审核日志
        log.setAuditId(327);
        shopAuditLogMapper.save(log);
        EmailUtil emailUtil = new EmailUtil();
        String from = "317191945@qq.com";
        String subject = "店铺入驻成功";
        String to = "1186095656@qq.com";
        String text = "店铺入驻成功，请手动点击链接进行激活：http://localhost:8080/shop/audit/\"+log.getShopId()";
        emailUtil.simpleEmail(from,to,subject,text);
//        // 发送激活邮箱
//        SimpleMailMessage mailMessage = new SimpleMailMessage();
//        //设置发送人
//        mailMessage.setFrom("317191945@qq.com");
//        //邮件主题
//        mailMessage.setSubject("店铺入驻成功");
//        //邮件内容
//        mailMessage.setText("店铺入驻成功，请手动点击链接进行激活：http://localhost:8080/shop/audit/"+log.getShopId());
//        //收件人
//        mailMessage.setTo("1186095656@qq.com");
//
//        javaMailSender.send(mailMessage);

    }

    @Override
    public void refuse(ShopAuditLog log) {
        // 修改店铺状态
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(3);
        shopMapper.update(shop);
        // 保存审核日志
        log.setAuditId(327);
        shopAuditLogMapper.save(log);
        // 发送激活邮箱
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("317191945@qq.com");
        //邮件主题
        mailMessage.setSubject("店铺入驻已拒绝");
        //邮件内容
        mailMessage.setText("店铺入驻失败！");
        //收件人
        mailMessage.setTo("1186095656@qq.com");

        javaMailSender.send(mailMessage);
    }

    @Override
    public void batchAdd(List<Shop> shops) {
        shopMapper.batchAdd(shops);
    }

    @Override
    public void updateState(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(1);
        shopMapper.update(shop);
    }

    @Override
    public void reject(ShopAuditLog log) {
        // 修改店铺状态
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(-1);
        shopMapper.update(shop);
        // 保存审核日志
        log.setAuditId(327);
        shopAuditLogMapper.save(log);
        // 发送激活邮箱
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("317191945@qq.com");
        //邮件主题
        mailMessage.setSubject("店铺入驻驳回");
        //邮件内容
        mailMessage.setText("<h1>审核失败</h1><br>"+
                "<span>失败原因："+log.getNote()+"</span><br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+log.getId()+"'>点击修改资料</a>");

        //收件人
        mailMessage.setTo("317191945@qq.com");

        javaMailSender.send(mailMessage);
    }

    @Override
    public List<ShopVo> loadCount() {
        return shopMapper.loadCount();
    }


}
